/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.DiemHocTapPOJO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author MinhHieu
 */
public class DiemHocTapDAO {
    
      
    public static boolean NhapDiem(String maHS,String maMH,String hocKi,String MaNamHoc,String  Diem15phut, String  Diem1tiet, String  DiemCK) {
        boolean kq = false;
        try {
            String sql = String.format("INSERT INTO bangdiem(MaHocSinh,MaMonHoc,HocKy,MaNamHoc,Diem15,Diem1Tiet,DiemCuoiKy) VALUES('%s','%s','%s','%s',%s,%s,%s)",maHS,maMH,hocKi,MaNamHoc,Diem15phut,Diem1tiet,DiemCK);
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            //ket noi
            helper.open();
            int a = helper.excecuteUpdate(sql);
            if(a==1){
                kq = true;
            }
            helper.close();

        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }
    public static ArrayList<DiemHocTapPOJO> laydiemht (){
        ArrayList<DiemHocTapPOJO> list = new ArrayList<DiemHocTapPOJO>();
        try {
            String sql = "SELECT* FROM DIEMHOCTAP";
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            //ket noi
            helper.open();
            //rut trich du lieu
            ResultSet rs = helper.excecuteQuery(sql);
            while (rs.next()) {
                DiemHocTapPOJO d = new DiemHocTapPOJO();
                d.setMaHS(rs.getString("MaHS"));
                d.setMaMH(rs.getString("MaMonHoc"));
                d.setHocKy(rs.getString("HocKy"));
                d.setDiem15(rs.getString("Diem15phut"));
                d.setDiem1Tiet(rs.getString("Diem1Tiet"));
                d.setDiemCuoiKy(rs.getString("DiemCuoiKy"));
                list.add(d);
            }
            helper.close();

        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return list;
    }
    ///hieu
    public static ArrayList<DiemHocTapPOJO> LayDiemHoctap(String MaHocSinh,String MaMon ,String HocKy, String MaNamHoc){
        ArrayList<DiemHocTapPOJO> list = new ArrayList<DiemHocTapPOJO>();
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("SELECT MaMonHoc, Diem15, Diem1Tiet, DiemCuoiKy "
                                     + "FROM bangdiem "
                                     + "WHERE MaHocSinh='%s' AND HocKy='%s' AND MaNamHoc='%s' AND MaMonHoc ='%s'",MaHocSinh,HocKy,MaNamHoc,MaMon);
            ResultSet rs = helper.excecuteQuery(sql);
            while(rs.next()){
                DiemHocTapPOJO d = new DiemHocTapPOJO();
                d.setMaMH(rs.getString("MaMonHoc"));
                d.setDiem15(rs.getString("Diem15"));
                d.setDiem1Tiet(rs.getString("Diem1Tiet"));
                d.setDiemCuoiKy(rs.getString("DiemCuoiKy"));
                list.add(d);
            }
            
            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return list;
    }
    public static boolean SuaDiem(String maHS,String maMH,String hocKi,String MaNamHoc,String  Diem15phut, String  Diem1tiet, String  DiemCK) {
        boolean kq = false;
        try {
            String sql = String.format("UPDATE bangdiem set Diem15=%s,Diem1Tiet=%s,DiemCuoiKy=%s WHERE MaHocSinh='%s' and MaMonHoc='%s' and HocKy='%s' and MaNamHoc='%s'",Diem15phut,Diem1tiet,DiemCK,maHS,maMH,hocKi,MaNamHoc);
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            //ket noi
            helper.open();
            int a = helper.excecuteUpdate(sql);
            if(a==1){
                kq = true;
            }
            helper.close();

        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }

}
